﻿@using Egroo.UI.Models;
@using jihadkhawaja.chat.client.Services;

@inherits ProtectedViewBase

@inject SessionStorage SessionStorage
@inject NavigationManager NavigationManager
@inject StorageService StorageService

<MudContainer MaxWidth="MaxWidth.Large" Class="my-4 pt-4">
    <MudText Typo="Typo.h3" GutterBottom>Settings</MudText>
    <MudButton Color="Color.Error" Variant="Variant.Filled" EndIcon="@Icons.Material.Filled.Logout" OnClick="SignOut">LOGOUT</MudButton>
</MudContainer>

@code{
    [Inject] IDialogService DialogService { get; set; } = null!;

    private async Task SignOut()
    {
        bool? result = await DialogService.ShowMessageBox(
            "Warning",
            "Are you sure you want to logout?",
            yesText: "Logout", cancelText: "Cancel");

        if (!result.HasValue || !result.Value)
        {
            return;
        }

        switch (ClientModel.CurrentFrameworkPlatform)
        {
            case FrameworkPlatform.MAUI:
                string path = Path.Combine(ClientModel.AppDataPath);
                StorageService.DeleteDirectory(path);
                break;
            case FrameworkPlatform.WASM:
            case FrameworkPlatform.SERVER:
                await StorageService.RemoveFromLocalStorage("token");
                break;
            default:
                throw new Exception("CurrentFrameworkPlatform parameter is undefined.");
        }

        SessionStorage.User = null;

        NavigationManager.NavigateTo("/", true);
    }
}
